/******************************************************************************** /
 * AppConfiguration.java - JPLearning Project												*
 * 																				*
 * 		This program is free software; you can redistribute it and/or modify	*
 *		it under the terms of the GNU General Public License as published by	*
 *		the Free Software Foundation; version 2 of the License.					*
 *																				*
 *		This program is distributed in the hope that it will be useful,			*
 *		but WITHOUT ANY WARRANTY; without even the implied warranty of			*
 *		MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the			*
 *		GNU General Public License for more details.							*
 *																				*
 ********************************************************************************/
package org.jplearning.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;

/**
 * @author A144601
 */
public class AppConfiguration implements IAppConfigurationListener{
	private static AppConfiguration _instance;
	private Properties properties;
	private Hashtable changesNotSaved;
	private List appConfigurationListeners;
	
	private AppConfiguration(String file) throws IOException{
		Properties properties = new Properties();
		
		InputStream inputStream;
		File confFile = new File(file);
		try {
			inputStream = new FileInputStream(confFile);
		} catch (FileNotFoundException e) {
			confFile.createNewFile();
			// TODO: Copiar la configuracion por defecto a la carpeta de configuracion
			inputStream = new FileInputStream(confFile);
		}
		properties.load(inputStream);
		changesNotSaved = new Hashtable();
		appConfigurationListeners = new LinkedList();
	}
	
	public static void configure(String file) throws IOException{
		if(_instance == null){
			_instance = new AppConfiguration(file);
		}
	}
	
	public static AppConfiguration getInstance(){
		if(_instance == null){
			throw new ExceptionInInitializerError(Constants.CLASS_NOT_INITIALIZED);
		}
		return _instance;
	}
	
	public String getString(String prop){
		String val;
		if(changesNotSaved.containsKey(prop)){
			val = (String)changesNotSaved.get(prop);
		}
		else{
			val = properties.getProperty(prop);
		}
		return val;
	}
	
	public int getInt(String prop){
		return new Integer(getString(prop)).intValue();
	}
	
	public boolean getBoolean(String prop){
		return new Boolean(getString(prop).toLowerCase()).booleanValue();
	}
	
	public void setProperty(String prop, String val){
		changesNotSaved.put(prop, val);
		propertyChanged(prop, val);
	}
	
	public void setProperty(String prop, int val){
		String strVal = new Integer(val).toString();
		setProperty(prop, strVal);
	}
	
	public void setProperty(String prop, boolean val){
		String strVal = new Boolean(val).toString();
		setProperty(prop, strVal);
	}
	
	public void saveChanges(){
		Enumeration keyEnum = changesNotSaved.keys();
		while(keyEnum.hasMoreElements()){
			String key = (String)keyEnum.nextElement();
			String val = (String)changesNotSaved.get(key);
			properties.put(key, val);
		}
		changesNotSaved.clear();
	}
	
	public void deleteChanges(){
		Enumeration keyEnum = changesNotSaved.keys();
		while(keyEnum.hasMoreElements()){
			String key = (String)keyEnum.nextElement();
			String val = properties.getProperty(key);
			propertyChanged(key, val);
		}
		changesNotSaved.clear();
	}
	
	public void addAppConfigurationListener(IAppConfigurationListener listener){
		if(listener == this){
			throw new IllegalArgumentException("It can't be listening itself!");
		}
		appConfigurationListeners.add(listener);
	}
	
	public void removeAppConfigurationListener(IAppConfigurationListener listener){
		appConfigurationListeners.remove(listener);
	}
	
	public void propertyChanged(String property, String value) {
		Iterator itListeners = appConfigurationListeners.iterator();
		while(itListeners.hasNext()){
			IAppConfigurationListener curListener = 
				(IAppConfigurationListener)itListeners.next();
			curListener.propertyChanged(property, value);
		}
	}
}
